﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using System.Collections;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class frmProduct : Form
    {
        string _id="101";
        private BindingSource bsource = new BindingSource();
        public enum clicktype
        {
            Add = 1,
            Edit = 2,

        }
        clicktype ConsructorType = new clicktype();

        
        public frmProduct()
        {
            InitializeComponent();
            ConsructorType = clicktype.Add;
        }
        public frmProduct(Product _prod) 
        {
            ConsructorType = clicktype.Edit;
            InitializeComponent();
            _id = _prod.ProductId;
            textBox2.Text = _prod.ProductName;
            comboBox1.Text = _prod.ProductTypeId;
            textBox4.Text = _prod.ProductSize;
            textBox5.Text = _prod.ProductPrice;
            textBox6.Text = _prod.ProductToo;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRegAdd_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false)
            {
                timer1.Enabled = true;
                               
            }
            if(timer2.Enabled == true){
                if (ConsructorType == clicktype.Add)
                {
                    Product.AddFunction(_id, textBox2.Text, comboBox1.Text, textBox4.Text, textBox5.Text, textBox6.Text);
                    MessageBox.Show("Амжилттай нэмэгдлээ");
                    _id = "";
                    textBox2.Text = "";
                    comboBox1.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                    textBox6.Text = "";
                }
                else
                {

                    Product.EditFunction(_id, textBox2.Text, comboBox1.Text, textBox4.Text, textBox5.Text, textBox6.Text);
                    MessageBox.Show("Амжилттай засагдлаа");

                }
                dataGridViewProduct.DataSource = DBTools.selecQryTmp("Select *from Product");
           
            }
         


        }
        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewProduct.Columns[colname].HeaderText = caption;
            dataGridViewProduct.Columns[colname].DisplayIndex = index;
            dataGridViewProduct.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid()
        {
            dataGridViewProduct.AllowUserToOrderColumns = true;
            dataGridViewProduct.AllowUserToAddRows = false;
            dataGridViewProduct.AllowUserToDeleteRows = false;
            dataGridViewProduct.ReadOnly = true;
            dataGridViewProduct.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewProduct.MultiSelect = true;

            FormatColumn("ProductId", "Бүтээгдэхүүний код", 0);
            FormatColumn("ProductName", "Бүтээгдэхүүний нэр", 1);
            FormatColumn("ProductTypeId", "Бүтээгдэхүүний төрөл", 2);
            FormatColumn("ProductSize", "Бүтээгдэхүүний хэмжээ", 3);
            FormatColumn("ProductPrice", "Нэгж үнэ", 4);
            FormatColumn("ProductToo", "Нийт тоо", 5);
        }
        private void frmProduct_Load(object sender, EventArgs e)
        {

           
            dataGridViewProduct.DataSource = DBTools.selecQryTmp("Select *from Product");
            FormatGrid();
        

        }

     

        private void btnsearch_Click(object sender, EventArgs e)
        {

        }

        private void btnRegEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnsearch_Click_1(object sender, EventArgs e)
        {
                       
        }

        private void btnRegDel_Click(object sender, EventArgs e)
        {
            int i = 0;
            Product cls = new Product();

            i = dataGridViewProduct.CurrentRow.Index;
            DataRowView drv = dataGridViewProduct.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string byComputerrec = drv[0].ToString();

                Product.DelFunction(byComputerrec);
                MessageBox.Show("Амжилттай устгалаа");

                dataGridViewProduct.DataSource = DBTools.selecQryTmp("Select *from Product");
            }
        }

        private void txtproductcode_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridProduct_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

            dataGridViewProduct.DataSource = DBTools.selecQryTmp("Select *from Product");
        }

        private void dataGridViewProduct_DoubleClick(object sender, EventArgs e)
        {
            if (timer1.Enabled == true)
            {
                timer1.Enabled = true;

                int i = 0;
                Product cls1 = new Product();

                i = dataGridViewProduct.CurrentRow.Index;
                DataRowView drv = dataGridViewProduct.CurrentRow.DataBoundItem as DataRowView;

                if (drv != null)
                {
                    string byButCode = drv[0].ToString();

                    cls1 = Product.getbyRec(byButCode);
                    Form EA = new frmProduct(cls1);


                }
            }
           
                
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Width >= 1176) this.timer2.Enabled = true;
            else this.Width += 10;
        }

        private void btnSearch_Click_2(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                string aa = string.Format("select *from Product where {0}='{1}' ", "ProductId", txtFilter.Text);
                dataGridViewProduct.DataSource = DBTools.selecQryTmp(aa);
            }
            if (radioButton2.Checked == true)
            {
                string aa = string.Format("select *from Product where {0}='{1}' ", "ProductName", txtFilter.Text);
                dataGridViewProduct.DataSource = DBTools.selecQryTmp(aa);
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
          
        }

        private void edit_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false)
            {
                timer1.Enabled = true;
                int i = 0;
                Product cls1 = new Product();

                i = dataGridViewProduct.CurrentRow.Index;
                DataRowView drv = dataGridViewProduct.CurrentRow.DataBoundItem as DataRowView;

                if (drv != null)
                {
                    string byButCode = drv[0].ToString();

                    cls1 = Product.getbyRec(byButCode);
                    frmProduct EA = new frmProduct(cls1);


                }

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

        }

       
    }
}
